/*
 * File Name：Microphone.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： Microphone.java
 * Modify By：pla-gysu
 * Modify Date：2015年1月21日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.utils.BaseUtils;


/**
 * 麦克风
 * 
 * @author pla-gysu
 * @version ciwong v.1.0 2015年1月21日
 * @since ciwong v.1.0
 */
public class Microphone extends View
{

    private Paint mPaint;

    private Drawable image;

    private int progressColor;

    private float mPainStroke;

    private RectF oval;

    private float sweepAngle;

    public Microphone(Context context)
    {
        super(context);
        init();
    }

    public Microphone(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.microphone);
        init(a);
        init();
    }

    public Microphone(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.microphone);
        init(a);
        init();
    }

    private void init(TypedArray a)
    {
        progressColor = a.getColor(R.styleable.microphone_microphone_color,
                Color.rgb(152, 212, 119));
        mPainStroke = (int) a.getDimension(
                R.styleable.microphone_microphone_stroke, 10);
        DisplayMetrics metrics = BaseUtils.getDisPlayMetrics(getContext());
        mPainStroke = mPainStroke / metrics.scaledDensity + 0.5f;
        image = a.getDrawable(R.styleable.microphone_microphone_image);
        a.recycle();
    }

    private void init()
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(progressColor);
        mPaint.setStrokeWidth(mPainStroke);
    }

    @Override
    public void draw(Canvas canvas)
    {
        super.draw(canvas);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (image != null)
        {
            LayoutParams lp = getLayoutParams();
            int w = lp.width == LayoutParams.WRAP_CONTENT ? image
                    .getIntrinsicWidth() : getMeasuredWidth();
            lp.width = w;
            int h = lp.height == LayoutParams.WRAP_CONTENT ? image
                    .getIntrinsicHeight() : getMeasuredHeight();
            lp.height = h;
            setLayoutParams(lp);
            Rect r = new Rect(getPaddingLeft(), getPaddingTop(), w
                    - getPaddingRight(), h - getPaddingBottom());
            oval = new RectF(r);
            oval.left += 5;
            oval.top += 5;
            oval.right -= 5;
            oval.bottom -= 5;
            image.setBounds(r);
            image.draw(canvas);
        }
        if (oval != null)
        {
            canvas.drawArc(oval, 0, sweepAngle, false, mPaint);
        }
    }

    /**
     * 设置录制时的分贝
     * 
     */
    public void setRms(double rms)
    {
        System.out.println(rms);
        float temp = (float) rms / 100;
        sweepAngle = temp * 360;
        if (sweepAngle < 0)
        {
            sweepAngle = 0;
        }
        postInvalidate();
    }
}
